/*
 * Copyright (C) 2009 AnK Team
 *	 Andrey Korolev
 *	 Anna Krel
 *	 Anna Kapanina
 *	 
 *	 http://code.google.com/p/ankfood/	
 *    
     
 *    Licensed under the GNU General Public License, Version 2.0 (the "License");
 *
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *         http://www.gnu.org/licenses/gpl-2.0.html

 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,

 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package org.ank.food;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class AdviceManager {
	
	protected final static int[] NOT_DESIRABLE_GROUPS = {19,21,22,25,35};
	protected final static String[] NOT_DESIRABLE_NAMES = {"alcohol"};
	public final static String QUOTAS_EXEEDED_STRING = "_1";
	public final static String QUOTAS_EXEEDED_PROT_STRING = "_2";
	public final static String QUOTAS_EXEEDED_FAT_STRING = "_3";
	public final static String QUOTAS_EXEEDED_CARBO_STRING = "_2";
	
    protected ContentValues values;
    protected Context context;
//	protected int currentProfileId;
    
	protected volatile static AdviceManager instance;
	
	protected AdviceManager(Context c) {
		this.values = new ContentValues();
		this.context = c;
	}
 
    public static AdviceManager getInstance(Context c) {
        if (instance == null) {
            synchronized (AdviceManager.class) {
                if (instance == null) {
                    instance = new AdviceManager(c);
                    
                }
            }
        }
        return instance;
    }
    
//    public void setCurrentProfileId(int id){
//    	////Log.v("advice","current profile is "+id);
//    	currentProfileId = id;
//    }
    

    public void deleteProfile(int id){
//    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_ADVICES, 
//    			AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID]+"="+String.valueOf(id), null);
    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_EATEN, 
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID]+"="+String.valueOf(id), null);
    }
    
    protected boolean groupIsAcceptable(int group){
		int i;
		for (i = 0; i < NOT_DESIRABLE_GROUPS.length; i++)
			if ( group == NOT_DESIRABLE_GROUPS[i])
				return false;
		return true;
	}
    
    protected boolean productNameIsAcceptable(String name){
		int i;
		for (i = 0 ; i < NOT_DESIRABLE_NAMES.length; i++)
			if (name.toLowerCase().contains(NOT_DESIRABLE_NAMES[i]))
				return false;
		return true;
	}
    
    public void saveNewAdvice(/*Cursor productsCursor*/String[] productNames){
    	int productId=0,profileId=0;
    	String productName;
    	////Log.v("advice","save new advice");

    	//delete info about old advice
    	clearAdvice();

    	//save new advice
    	int count = productNames.length;
    	values = new ContentValues();
    	for(int i = 0 ; i < count ; i++){
    		productId = ProductDataManager.getInstance(context).getProductIdByName(productNames[i]);
    		profileId = ProfileManager.getInstance(context).getCurrentProfileId();
    		productName = productNames[i];
    		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
    			findMaxIndex(AdviceProvider.ADVICES)+1 );
    		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
        			productId );
    		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID],
        			profileId );
    		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
        			productName );
    		
    		context.getContentResolver().insert(AdviceProvider.CONTENT_URI_ADVICES, values);
    	}
    	
    }
    
    public void saveNewAdviceItem(String[] productInfo){
    	values = new ContentValues();
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
    			findMaxIndex(AdviceProvider.ADVICES)+1 );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
        			Integer.parseInt(productInfo[0]) );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID],
    			ProfileManager.getInstance(context).getCurrentProfileId() );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
        			productInfo[1] );
    	context.getContentResolver().insert(AdviceProvider.CONTENT_URI_ADVICES, values);
    }
    
   
    
    
    public void deleteAdviceItem(int id){
    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_ADVICES, 
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.ID]+" = "+
    			String.valueOf(id), null);
    }
    
    public void moveFromAdviceToEaten(int adviceId){
    	Cursor c = context.getContentResolver().query(AdviceProvider.CONTENT_URI_ADVICES, 
    			new String[]{AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID]},
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.ID]+" = "+String.valueOf(adviceId), 
    			null,null);
    	c.moveToFirst();
    	saveNewEaten(c.getInt(0));
    	deleteAdviceItem(adviceId);
    }
    
    public void saveNewEaten(String productName){
    	int productId=0,profileId=0;
//    	////Log.v("advice","save new eaten");
    	Cursor currentProductCursor;
    	currentProductCursor = context.getContentResolver().query(ProductDataProvider.CONTENT_URI_PRODUCTS,
    			new String[]{ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.ID]},
				ProductDataProvider.COLUMN_NAMES_PRODUCTS[ProductDataProvider.NAME]+" = \""+productName+"\"", 
				new String[]{}, null);
    	currentProductCursor.moveToFirst();
    	productId = currentProductCursor.getInt(0);
    	currentProductCursor.close();
    	
   		profileId = ProfileManager.getInstance(context).getCurrentProfileId();
   		values = new ContentValues();
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
    		findMaxIndex(AdviceProvider.EATEN)+1 );
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
        			productId );
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID],
        			profileId );
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
        			productName );
    		
   		context.getContentResolver().insert(AdviceProvider.CONTENT_URI_EATEN, values);
    	
    }
    
    public void saveNewEaten(int productId){
    	int profileId=0;
    	
    	String productName = ProductDataManager.getInstance(context).getProductNameById(productId);
   		profileId = ProfileManager.getInstance(context).getCurrentProfileId();
   		values = new ContentValues();
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
    		findMaxIndex(AdviceProvider.EATEN)+1 );
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
        			productId );
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID],
        			profileId );
   		values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
        			 productName);
    		
   		context.getContentResolver().insert(AdviceProvider.CONTENT_URI_EATEN, values);
    	
    }
    
    public void saveNewEaten(String[] productInfo){
    	values = new ContentValues();
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
    			findMaxIndex(AdviceProvider.EATEN)+1 );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
        			Integer.parseInt(productInfo[0]) );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID],
    			ProfileManager.getInstance(context).getCurrentProfileId() );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
        			productInfo[1] );
    	////Log.v("saveNewEaten", "productinfo is "+productInfo[0]+", "+productInfo[1]);
    	////Log.v("saveNewEaten", "values are "+values);
    	context.getContentResolver().insert(AdviceProvider.CONTENT_URI_EATEN, values);
    }
    
    public void saveNewMenuItem(int productId, String productName,
    		String weightItemName, int weightItemAmount, float weightItemWeight){
    	int currentProfileId = ProfileManager.getInstance(context).getCurrentProfileId();
    	saveNewEaten(currentProfileId, productId, productName, weightItemName, 
    			weightItemAmount, weightItemWeight);
    	
    }
    
    public void saveNewMenuItem(String[] productInfo, String weightItemName, 
    		int weightItemAmount, float weightItemWeight){
    	int currentProfileId = ProfileManager.getInstance(context).getCurrentProfileId();
    	int productId = Integer.parseInt(productInfo[1]);
    	saveNewEaten(currentProfileId, productId, productInfo[1], weightItemName, 
    			weightItemAmount, weightItemWeight);
    	
    }
    
    public void saveNewMenuItem(int productId, int weightItemId, int weightItemAmount){
    	ProductDataManager pm = ProductDataManager.getInstance(context); 
    	int currentProfileId = ProfileManager.getInstance(context).getCurrentProfileId();
    	String productName = pm.getProductNameById(productId);
    	String weightItemName = pm.getWeightItemNameById(weightItemId);
    	float weightItemWeight = pm.getWeightItemWeightById(weightItemId);
    	
    	saveNewEaten(currentProfileId, productId, productName, weightItemName, 
    			weightItemAmount, weightItemWeight);
    }
    
    public void saveNewEaten(int profileId, int productId, String productName,
    		String weightItemName, int weightItemAmount, float weightItemWeight){
    	values = new ContentValues();
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
    			findMaxIndex(AdviceProvider.EATEN)+1 );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
        		productId );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID],
    			profileId );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
        		productName );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_NAME],
        		weightItemName );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_AMOUNT],
        		weightItemAmount );
    	values.put(AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_WEIGHT],
        		weightItemWeight );
//    	////Log.v("saveNewEaten", "productinfo is "+productInfo[0]+", "+productInfo[1]);
//    	////Log.v("saveNewEaten", "values are "+values);
    	context.getContentResolver().insert(AdviceProvider.CONTENT_URI_EATEN, values);
    }
    
    public void deleteEaten(String name){
    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_EATEN, 
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID] + " = "+String.valueOf(
						ProfileManager.getInstance(context).getCurrentProfileId())+" AND "+
						AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME] + " = \""+name+"\"", 
    			null);
    }
    
    public void deleteEaten(int id){
    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_EATEN, 
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.ID] + " = "+String.valueOf(id), 
    			null);
    }
    
    public void clearEaten(){
    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_EATEN, 
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID] + " = "+String.valueOf(
						ProfileManager.getInstance(context).getCurrentProfileId()), 
    			null);
    }
    
    public void clearAdvice(){
    	context.getContentResolver().delete(AdviceProvider.CONTENT_URI_ADVICES, 
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID] + " = "+String.valueOf(
						ProfileManager.getInstance(context).getCurrentProfileId()), 
    			null);
    }
    
    public Cursor getLastAdviceCursor(){
		String[] returnFields = new String[]{
				AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
				AdviceProvider.COLUMN_NAMES[AdviceProvider.ID]
		};
		return context.getContentResolver().query(AdviceProvider.CONTENT_URI_ADVICES, 
				returnFields, AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID]+" = "
				+String.valueOf( ProfileManager.getInstance(context).getCurrentProfileId() ), 
				new String[]{}, null);
	}
    
    
    
    protected int getEatenProtEnergy(){
    	int result = 0, prodId, weightId;
    	Cursor eatenCursor = getEaten();
    	eatenCursor.moveToFirst();
    	int legth = eatenCursor.getCount();
    	for(int i = 0; i < legth; i++){
    		eatenCursor.moveToPosition(i);
    		prodId = eatenCursor.getInt(2);
    		
    		result += ProductDataManager.getInstance(context).getProductProtEnergy(prodId)*
    			eatenCursor.getInt(3)*Math.round(eatenCursor.getFloat(5))/
    			ProductDataManager.STANDARD_WEIGHT; 
    	}
    	eatenCursor.close();
    	return result;
    }
    protected int getEatenFatEnergy(){
    	int result = 0, prodId;
    	Cursor eatenCursor = getEaten();
    	eatenCursor.moveToFirst();
    	int legth = eatenCursor.getCount();
    	for(int i = 0; i < legth; i++){
    		eatenCursor.moveToPosition(i);
    		prodId = eatenCursor.getInt(2);
    		result += ProductDataManager.getInstance(context).getProductFatEnergy(prodId)*
			eatenCursor.getInt(3)*Math.round(eatenCursor.getFloat(5))/
			ProductDataManager.STANDARD_WEIGHT;
    	}
    	eatenCursor.close();
    	return result;
    }
    protected int getEatenCarboEnergy(){
    	int result = 0, prodId;
    	Cursor eatenCursor = getEaten();
    	eatenCursor.moveToFirst();
    	int legth = eatenCursor.getCount();
    	for(int i = 0; i < legth; i++){
    		eatenCursor.moveToPosition(i);
    		prodId = eatenCursor.getInt(2);
    		result += ProductDataManager.getInstance(context).getProductCarboEnergy(prodId)*
			eatenCursor.getInt(3)*Math.round(eatenCursor.getFloat(5))/
			ProductDataManager.STANDARD_WEIGHT;
    	}
    	eatenCursor.close();
    	return result;
    }
	
	
    //the method returns int[3], where the first element indicates the atate of protein qouta,
    //the second - fat qouta, the third - carbohydrate qouta; 0 means not exceeded, 1 means exceeded
    public int[] makeAdvice(){
		int[] result = /*new int[3]*/{0,0,0};
//		String resultItem;
		int protQuota, fatQuota, carboQuota;
		int group;
		String[] productInfo;
		
		//delete old advice
		clearAdvice();
		
		//get quotas from profile info
		protQuota = ProfileManager.getInstance(context).getProtQuota();
		fatQuota = ProfileManager.getInstance(context).getFatQuota();
		carboQuota = ProfileManager.getInstance(context).getCarboQuota();
//		////Log.v("getAdvice","quotas from profileM = "+protQuota+" "+fatQuota+" "+carboQuota);
		
		/*
		subtract amounts taken from EatenToday info

		 */
		boolean exceeded = false;
		protQuota -= getEatenProtEnergy();
		if (protQuota <= 0){
			result[0] = 1;
			exceeded = true;
		}
		fatQuota -= getEatenFatEnergy();
		if (fatQuota <= 0){
			result[1] = 1;
			exceeded = true;
		}
		carboQuota -= getEatenCarboEnergy();
		if (carboQuota <= 0){
			result[2] = 1;
			exceeded = true;
		}
		
		if (exceeded){
			return result;
		}
	
		ProductDataManager.getInstance(context).setProtQuota(protQuota);
		ProductDataManager.getInstance(context).setFatQuota(fatQuota);
		ProductDataManager.getInstance(context).setCarboQuota(carboQuota);
		
		group = 1;
		int groupCounter = 0;
		int counter = 0;
		while(counter < 11){
			if(group >= ProductDataProvider.MAX_GROUP_ID)
				group = 1;
			else
				group++;
			if ( !groupIsAcceptable(group) )
				continue;
//			if ( (resultItem = ProductDataManager.getInstance(context).getSuitableProductNameAndId(group)) != null ){
//				if( !productNameIsAcceptable(resultItem) )
//					continue;
//				String[] tmp = new String[result.length+1];
//				System.arraycopy(result, 0, tmp, 0, result.length);
//				tmp[tmp.length-1]=resultItem; //add new product name
//				result = tmp;
//			}else{
//				groupCounter++;
//				if (groupCounter > ProductDataProvider.MAX_GROUP_ID)  //cant find suitable product in all groups
//					break;
//			}
			if ( (productInfo = ProductDataManager.getInstance(context).
					getSuitableProductNameAndId(group)) != null ){
				if( !productNameIsAcceptable(productInfo[1]) )
					continue;
				saveNewAdviceItem(productInfo);
				saveNewEaten(productInfo);
			}else{
				groupCounter++;
				if (groupCounter > ProductDataProvider.MAX_GROUP_ID)  //cant find suitable product in all groups
					break;
			}
			
		}
		return result;
	}
    
	public Cursor getEaten(){
		String[] returnFields = {
			AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME],
			AdviceProvider.COLUMN_NAMES[AdviceProvider.ID],
			AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_ID],
			AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_AMOUNT],
			AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_NAME],
			AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_WEIGHT]
		};
//		////Log.v("getEaten", "cur_profile_id = "+String.valueOf(
//				ProfileManager.getInstance(context).getCurrentProfileId()));
		return context.getContentResolver().query(AdviceProvider.CONTENT_URI_EATEN, 
				returnFields, 
				AdviceProvider.COLUMN_NAMES[AdviceProvider.PROFILE_ID]+" = "+String.valueOf(
						ProfileManager.getInstance(context).getCurrentProfileId()),
				null, null);
	}
	
	public void setExceedings(int [] data){
		if (data.length != 3)
			throw new IllegalArgumentException();
		setExceedings(data[0], data[1], data[2]);
	}
	public void setExceedings(int protExceeding, int fatExceeding, int carboExceeding){
		//clear old info
		int currentProfileId = ProfileManager.getInstance(context).getCurrentProfileId();
		context.getContentResolver().delete(AdviceProvider.CONTENT_URI_EXCEEDINGS, 
				AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.PROFILE_ID]+
				" = " + String.valueOf(currentProfileId), null);
		//save new info
		ContentValues values = new ContentValues();
		values.put(AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.ID], 
				findMaxIndex(AdviceProvider.EXCEEDINGS));
		values.put(AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.PROFILE_ID], 
				currentProfileId);
		values.put(AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.PROT_EXCEEDING], 
				protExceeding);
		values.put(AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.FAT_EXCEEDING], 
				fatExceeding);
		values.put(AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.CARBO_EXCEEDING], 
				carboExceeding);
		context.getContentResolver().insert(AdviceProvider.CONTENT_URI_EXCEEDINGS, values);
	}
	
	public int[] getExceedings(int profileId){
		int[] result = new int[3];
		Cursor c = context.getContentResolver().query(AdviceProvider.CONTENT_URI_EXCEEDINGS, 
				new String[]{
					AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.PROT_EXCEEDING],
					AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.FAT_EXCEEDING],
					AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.CARBO_EXCEEDING]
				}, AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.PROFILE_ID]+ " = "+
				String.valueOf(profileId), null, null);
		c.moveToFirst();
		for(int i = 0; i < result.length; i++)
			result[i] = c.getInt(i);
		return result;
	}
	
	public boolean makeAndSaveSingleAdvice(int groupId){
//		int protQuota, fatQuota, carboQuota;
//		String[] productInfo;
//
//		//get quotas from profile info
//		ProfileManager prfM = ProfileManager.getInstance(context); 
//		ProductDataManager prdM = ProductDataManager.getInstance(context);
//		protQuota = prfM.getProtQuota();
//		fatQuota = prfM.getFatQuota();
//		carboQuota = prfM.getCarboQuota();
////		////Log.v("getAdvice","quotas from profileM = "+protQuota+" "+fatQuota+" "+carboQuota);
//		
//		/*
//		subtract amounts taken from EatenToday info
//
//		 */
//		protQuota -= getEatenProtEnergy();
//		fatQuota -= getEatenFatEnergy();
//		carboQuota -= getEatenCarboEnergy();
//	
//		prdM.setProtQuota(protQuota);
//		prdM.setFatQuota(fatQuota);
//		prdM.setCarboQuota(carboQuota);
//
//		boolean nameIsAcceptable = false;
//		productInfo = null;
//		while( !nameIsAcceptable ){
//			if ( (productInfo = prdM.getSuitableProductNameAndId(groupId)) != null ){
//				nameIsAcceptable = productNameIsAcceptable(productInfo[1]);
//			}else{
//				break;
//			}
//		}
//		if (productInfo != null){
////			////Log.v("advice","saving new advice: "+productInfo[0]);
//			saveNewEaten(productInfo);
//			return true;
//		}
////		////Log.v("advice","saving new advice");
//		return false;	
		
			ProductDataManager prdM = ProductDataManager.getInstance(context);
			Cursor c = prdM.getProductsByGroup(groupId);
			int pos, prodId, counter = 0;
			while(  counter  <  (c.getCount())  ){
				pos = (int)Math.round( Math.floor(    Math.random() * c.getCount()   ) );
//				pos = counter;
//				Log.v("saveadvise","c.getCount = "+c.getCount());
//				Log.v("saveadvise","pos = "+pos);
				c.moveToPosition(pos);
				prodId = c.getInt(1);
//				Log.v("saveadvise","prodID = "+prodId);
				int[] prodInfo;
				
				if ( ( prodInfo = prdM.getRecommendedWeightAsIntArr(prodId) )  != null){
					c.close();
					saveNewMenuItem(prodId, prodInfo[0], 1);
					return true;
				}
				counter++;
			}
			c.close();
			return false;
		
	}
    
	
    //this metod returns the max index - it is needed to insert a new row in the database
    protected int findMaxIndex(int identifier){
    	Cursor c;
    	String idColumnName;
    	Uri uri;
    	switch (identifier){
    	case AdviceProvider.ADVICES :
    		uri = AdviceProvider.CONTENT_URI_ADVICES;
    		idColumnName = AdviceProvider.COLUMN_NAMES[AdviceProvider.ID];
    		break;
    	case AdviceProvider.EATEN :
    		uri = AdviceProvider.CONTENT_URI_EATEN;
    		idColumnName = AdviceProvider.COLUMN_NAMES[AdviceProvider.ID];
    		break;
    	case AdviceProvider.EXCEEDINGS :
    		uri = AdviceProvider.CONTENT_URI_EXCEEDINGS;
    		idColumnName = AdviceProvider.COLUMN_NAMES_EXCEEDINGS[AdviceProvider.ID];
    		break;
    	default :
    		idColumnName = null;
    		uri = null;
    	 	////Log.v("AdviceManager","wrong identifier in findMaxIndex");
    	}
    	c = context.getContentResolver().query(uri, 
    			new String[]{ "max(" +idColumnName+ ") max_id" },
    			null, null, null);
    	if (c.getCount() == 0)
    		return 0;
    	c.moveToFirst();
    	int a = c.getInt(0);
    	c.close();
    	return a;
    }
    
    public int getEatenItemAmount(int id){
    	String[] returnFields = {
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_AMOUNT]
    		};
    		Cursor c = context.getContentResolver().query(AdviceProvider.CONTENT_URI_EATEN, 
    				returnFields, 
    				AdviceProvider.COLUMN_NAMES[AdviceProvider.ID]+" = "+id,
    				null, null);
    		c.moveToFirst();
     		int a = c.getInt(0); 
    		c.close();
    		return a;
    }
    
    public String getEatenItemWeightName(int id){
    	String[] returnFields = {
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.WEIGHT_ITEM_NAME]
    		};
    		Cursor c = context.getContentResolver().query(AdviceProvider.CONTENT_URI_EATEN, 
    				returnFields, 
    				AdviceProvider.COLUMN_NAMES[AdviceProvider.ID]+" = "+id,
    				null, null);
    		c.moveToFirst();
     		String s = c.getString(0); 
    		c.close();
    		return s;
    }
    
    public String getEatenItemProductName(int id){
    	String[] returnFields = {
    			AdviceProvider.COLUMN_NAMES[AdviceProvider.PRODUCT_NAME]
    		};
    		Cursor c = context.getContentResolver().query(AdviceProvider.CONTENT_URI_EATEN, 
    				returnFields, 
    				AdviceProvider.COLUMN_NAMES[AdviceProvider.ID]+" = "+id,
    				null, null);
    		c.moveToFirst();
     		String s = c.getString(0); 
    		c.close();
    		return s;
    }
    
    
    
    
    
}
